import socket
import threading


def return_msg(conn,addr):
    print('accept connections from',addr)
    while True:
        msg = conn.recv(1024)
        msg = msg.decode('utf8')
        if msg == "fin":
            conn.send(b'close connection')
            conn.close()
            break
        return_msg = 'accept your msg {}'.format(msg)
        conn.send(return_msg.encode('utf8'))
        print(msg)
    print('connection closed')
IP = '127.0.0.1'
PORT = 1953
address = (IP, PORT)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(address)
server.listen(18)
while 1:
    conn, addr = server.accept()
    threading.Thread(target=return_msg, args=(conn, addr)).start()